<?php 

/**
* 
*/
class GoodsModel extends Model
{
	public $sortarr = array(1 => '钻石卡', 2 => '金卡', 3 => '银卡', );
    public $sortArrCode = array(1 => 'D', 2 => 'G', 3 => 'S', );
    public $categray = array(1=>'数码家电',2=>'家居家纺',3=>'商务礼品',4=>'时尚潮品',99=>'其他');

	public function getGoods($condition,$offset,$limit) {
		$map = array();

		if(isset($offset) && isset($limit)){
			$map['limit'] = $offset . ',' . $limit;
		}
		$map['order'] = "id desc";
		//$this->where($map)->limit($offset,$limit)->select();
		//$this->select(array('order'=>'create_time','where'=>'status=1','limit'=>'10'));
		
		$data = $this->select($map);
		return $data;
	}

	/**
	 * 根据SNCODE码获取商品
	 * @param  [type] $sncode [description]
	 * @return [type]         [description]
	 */
	public function getGoodsByCode($sncode)
	{
		$row = $this->where(array('sncode'=>$sncode))->find();

		return $row ? $row : array();
	}

	/**
	 * 设置商品已领取
	 * @param [type] $sncode [description]
	 */
	public function setGoodsUnable($sncode)
	{
		$ret = $this->where(array('sncode'=>$sncode,'status'=>1))->save(array('status'=>0));
		return $ret;
	}

	/**
	 * 检查商品是否被领取
	 * @param  [type] $sncode [description]
	 * @return [type]         [description]
	 */
	public function checkGoodsValid($sncode)
	{
		$ret = $this->where(array('sncode'=>$sncode))->field('status,deadline')->find();
		return $ret;
	}

	/**
	 * 根据条件获取商品的总数
	 * @param  array  $map [description]
	 * @return [type]      [description]
	 */
	public function getGoodsCount($map = array())
	{
		//到时候可能会根据条件加缓存
		return $this->where($map)->count();
	}

	/**
	 * 根据条件获取商品的金额总数
	 * @param  array  $map [description]
	 * @return [type]      [description]
	 */
	public function getGoodsSum($map = array())
	{
		//到时候可能会根据条件加缓存
		return $this->where($map)->sum('saleprice');
	}

    public function getSortarr()
    {
        return $this->sortarr;
    }

    /**
     * 砖石卡等对应的商品码字母前缀
     * @return array
     */
    public function getSortArrCode()
    {
        return $this->sortArrCode;
    }

    /**
     * 产品分类
     * @return array
     */
    public function getCategray()
    {
        return $this->categray;
    }

    public function addGoods($title,$content,$price,$saleprice,$img,$status=1,$item,$sort,$categray,$parent_id=0,$deadline=0)
    {
        //组装
        $data['title'] = $title;
        $data['content'] = $content;
        $data['price'] = $price;
        $data['saleprice'] = $saleprice;
        $data['img'] = json_encode($img);
        $data['status'] = $status;
        $data['item'] = $item;
        $data['sort'] = $sort;
        $data['categray'] = $categray;
        $data['parent_id'] = $parent_id;
        $data['deadline'] = $deadline;
        $data['create_time'] = $data['update_time'] = time();
        //入库
        $InstId = M('Goods')->add($data);
        return $InstId;
    }
}
 ?>